<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称：</div>
      <el-input
        v-model="params.enterpriseName"
        clearable
        placeholder="请输入内容企业名称"
        class="search-main"
        @clear="clearSearch"
      />
      <div class="search-label">缴费时间：</div>
      <el-date-picker
        v-model="createTime"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-button type="primary" @click="doSearch">查询</el-button>
    </div>
    <!-- 添加区域 -->
    <div class="create-container">
      <el-button type="primary" @click="tianjia()">添加账单</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="goodList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="账单编号" width="190" prop="billNumber" />
        <el-table-column label="企业名称" width="190" prop="enterpriseName" />
        <el-table-column label="租赁楼宇" prop="buildingName" />
        <el-table-column label="物业费" prop="propertyFeePrice" />
        <el-table-column label="账单金额" prop="paymentAmount" />
        <el-table-column label="缴费时间" width="190" prop="createTime" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="check(scope.row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @current-page="pageChange"
      />
    </div>
    <!-- 弹框 -->
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
      @open="openDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="选择租户" prop="enterpriseId">
            <el-select v-model="addForm.enterpriseId" placeholder="请选择选择租户">
              <el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="租赁楼宇" prop="buildingId">
            <el-select v-model="addForm.buildingId" placeholder="请选择租赁楼宇">
              <el-option
                v-for="item in buildList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="缴费周期" prop="rentTime">
            <el-date-picker
              v-model="addForm.rentTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item label="支付金额" prop="paymentAmount">
            <el-input v-model="addForm.paymentAmount" :disabled="true" placeholder="请输入支付金额" />
          </el-form-item>
          <el-form-item label="支付方式" prop="paymentMethod">
            <el-select v-model="addForm.paymentMethod" placeholder="请选择支付方式">
              <el-option
                v-for="item in roleList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { budgetAPI, getdetailAPI, deldetailAPI, getbuildAPI, enterpriseNameAPI, createdAPI, checkAPI } from '@/api/propert'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        Authorization: ''
      },
      addForm: {
        enterpriseName: '', // 企业名称
        buildingName: '', // 楼宇名称
        paymentMethod: null, // 支付方式
        paymentAmount: null, // 付款总额
        startTime: '', // 缴费周期-开始时间
        endTime: '', // 缴费周期-结束时间
        enterpriseId: null, // 企业id
        buildingId: null, // 楼宇id
        rentTime: []// 时间
      },
      addFormRules: {
        roleId: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ]
      },
      // 支付方式
      roleList: [
        { value: 1,
          label: '微信'
        }, {
          value: 3,
          label: '现金'
        }, {
          value: 2,
          label: '支付宝'
        }
      ],
      title: '',
      total: 0,
      goodList: [], // 页面
      dialogVisible: false, // 控制弹框打开关闭
      userList: [], // 选择用户
      buildList: [], // 租赁楼宇
      createTime: '',
      abc: 0
    }
  },
  created() {
    this.pooList()
    // this.money()
  },
  methods: {
    // 预算金额
    async money() {
      const res = await budgetAPI({
        buildingId: this.addForm.buildingId,
        startTime: this.addForm.rentTime[0],
        endTime: this.addForm.rentTime[1]
      })
      this.abc = res.data
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.pooList()
    },
    clearSearch() {
      this.params.page = 1
      this.pooList()
    },
    // 搜索
    doSearch() {
      this.pooList()
      this.params.page = 1
    },
    // 查看
    async check(id) {
      this.title = '查看账单'
      this.dialogVisible = true
      const res = await checkAPI(id)
      const { enterpriseId, buildingId, paymentAmount, startTime, endTime, paymentMethod } = res.data
      this.addForm = { enterpriseId, buildingId, paymentAmount, startTime, endTime, paymentMethod }
      this.addForm.rentTime = [this.addForm.startTime, this.addForm.endTime]
    },
    // 确定提交
    async confirmAdd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.addForm.id) {
            console.log('修改')
          } else {
            await createdAPI({
              enterpriseId: this.addForm.enterpriseId,
              buildingId: this.addForm.buildingId,
              paymentAmount: this.addForm.paymentAmount,
              paymentMethod: this.addForm.paymentMethod,
              startTime: this.addForm.rentTime[0],
              endTime: this.addForm.rentTime[1]
            })
          }
        }
        this.$message({
          message: `${this.id ? '编辑' : '添加'}成功`,
          type: 'success'
        })
        this.pooList()// 刷新页面
        this.dialogVisible = false // 控制弹框打开关闭
      })
    },
    // 添加账单
    tianjia() {
      this.dialogVisible = true
      this.title = '添加账单'
    },
    // 关闭弹框
    closeDialog() {
      this.dialogVisible = false
      this.addForm = {
        paymentMethod: null, // 支付方式
        paymentAmount: null, // 付款总额
        startTime: '', // 缴费周期-开始时间
        endTime: '', // 缴费周期-结束时间
        enterpriseId: null, // 企业id
        buildingId: null, // 楼宇id
        rentTime: []// 时间
      }
    },
    // 打开弹框
    async openDialog() {
      // 租赁楼宇
      const res = await getbuildAPI()
      this.buildList = res.data
      // 租赁企业
      const poo = await enterpriseNameAPI()
      this.userList = poo.data

      // const mo = await budgetAPI({
      //   buildingId: this.addForm.buildingId,
      //   startTime: this.addForm.rentTime[0],
      //   endTime: this.addForm.rentTime[1]
      // })
      // console.log(mo.data)
      // this.abc = mo.data
    },
    // 页面
    async pooList() {
      const res = await getdetailAPI(this.params)
      this.goodList = res.data.rows
      this.total = res.data.total
      // console.log(res.data.rows[0].createTime)
    },
    // 删除
    del(id) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log(id)
        await deldetailAPI(id)
        this.$message.success('删除成功')
        this.pooList()
      })
      // console.log(id)
    }
  }

}
</script>
<style lang="scss" scoped>
.department-container {
  padding: 10px;
  height: 100vh;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
